package MyProject1;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {

    public static void client(){
        Scanner scanner = new Scanner(System.in);
        while (true){
            screen();
            String operate = scanner.nextLine();
            if (operate.equals("login")){
                //进行登录操作
                loginFun();

            } else if (operate.equals("enroll")) {
                //进行注册操作
                enrollFun();

            }else if (operate.equals("exit")){
                System.out.println("成功退出，欢迎下次再来!!!!");
                break;
            }else {
                System.out.println("输出不合法!!!!!!!");
                System.out.println("请重新输入操作!!!!");
            }
        }
    }

    private static void screen(){
        System.out.println("=========== 欢迎来到登录界面 ==============================");
        System.out.println("=========== 登录功能: 请输入 login 操作 ===================");
        System.out.println("=========== 注册功能: 请输入 enroll 操作 ==================");
        System.out.println("=========== 退出此界面:请输入 exit 操作 ====================");
        System.out.print("请输入:");
    }

    private static void loginFun(){
        System.out.println("========= 欢迎来到登录页面 ==============");
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入登录名: ");
        String loginName = scanner.nextLine();
        System.out.print("请输入密码: ");
        String password = scanner.nextLine();

        //接着将该登录名与密码请求到服务器中，TCP
        try (
                Socket socket = new Socket("127.0.0.1",9090);
                OutputStream os = socket.getOutputStream();
                InputStream is = socket.getInputStream();
                //将其升级为高级流
                DataInputStream dis = new DataInputStream(is);
                DataOutputStream dos = new DataOutputStream(os);
                ){
                String sent = loginName + "&" + password + "&" + "login";
                dos.writeUTF(sent);
            System.out.println("发送成功!!!!");

            //接下来需要接收服务器发送回来的信息
            String accept = dis.readUTF();
            if (accept.equals("true")){
                System.out.println("登录成功啦!!!!!!!!!!");
                success();

            }else {
                System.out.println("登录失败啦!!!!!!!!!!");

                System.out.println("自动退出登录页面，请重新输入登录名和密码!!!");

            }

        }catch (Exception e){
            e.printStackTrace();
        }



    }
    private static void success(){
        System.out.println("============机密==================");
        System.out.println("============机密==================");
        System.out.println("============机密==================");
        System.out.println("============机密==================");
        System.out.println("============机密==================");
        System.out.println("============机密==================");
        System.out.println("============机密==================");
        System.out.println("============机密==================");
    }

    private static void enrollFun(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("============  欢迎来到注册页面  ===============");
        System.out.print("请设置登录名称: ");
        String name = scanner.nextLine();
        System.out.print("请设置密码: ");
        String password = scanner.nextLine();

        //需要发送到服务端，查看是否重名，若不重名则进行保存在文件中
        try(
                Socket socket = new Socket("127.0.0.1",9090);
                InputStream is = socket.getInputStream();
                OutputStream os = socket.getOutputStream();
                //升级为高级流
                DataOutputStream dos = new DataOutputStream(os);
                DataInputStream dis = new DataInputStream(is);
                ){
            String sent = name + "&" + password + "&" + "enroll";
            dos.writeUTF(sent);

            //等待接受服务器返回来的数据
            String accept = dis.readUTF();
            if (accept.equals("true")){
                System.out.println("注册成功啦!!!!!");
            }else {
                System.out.println("已经有重名的啦!!!!");
                System.out.println("注册失败!!!!!!");
            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        client();
    }



}
